package com.yunzheng.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunzheng.entity.Project;
import com.yunzheng.exception.ServiceException;
import com.yunzheng.mapper.ProjectMapper;
import com.yunzheng.service.ProjectIterationService;
import com.yunzheng.util.SnowFlakeUtil;
import com.yunzheng.vo.IterationShowListVo;
import com.yunzheng.vo.IterationShowVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 李美琪
 * @date 2020/8/1 11:51
 */

@Service
public class ProjectIterationServiceImpl  extends ServiceImpl<ProjectMapper, Project> implements ProjectIterationService {

    @Resource
    private ProjectMapper projectMapper;

    /**
     * 新建迭代
     */
    @Override
    public void iterationPost(String projectSid, String projectName, Integer projectStatus, String description, LocalDateTime startTime, LocalDateTime finishTime) {

        // 判断相同项目下没有重复的迭代名
        int rows = projectMapper.selectCount(new QueryWrapper<Project>().eq(Project.COL_PROJECT_SID, projectSid).eq(Project.COL_PROJECT_NAME, projectName));
        if (rows > 0){
            throw new ServiceException("相同项目之下不能存在相同迭代名");
        }
//        //获得当前时间
//        LocalDateTime dateTime = LocalDateTime.now();
        Project entity = new Project();
        // 雪花算法生成迭代id
        String projectId = SnowFlakeUtil.nextId();
        //计算结束时间和开始时间的时间差
        long time = Duration.between(startTime,finishTime).toDays();
        if(time < 0){
            throw new ServiceException("结束时间早于开始时间");
        }
        String projectManager = projectMapper.selectById(projectSid).getProjectManager();
        entity.setProjectId(projectId).setProjectSid(projectSid).setProjectName(projectName).setProjectManager(projectManager).setProjectStatus(projectStatus).setDescription(description).setStartTime(startTime).setFinishTime(finishTime);
        projectMapper.insert(entity);

    }

    /**
     * 通过关键字搜索迭代
     */
    @Override
    public List<IterationShowListVo> iterationSeek(String forSeek, String projectId) {
        return projectMapper.getIterationSeek(forSeek, projectId);
    }

    /**
     * 迭代基本信息列表
     */
    @Override
    public List<IterationShowListVo> iterationShowList(String projectSid) {
        return projectMapper.getIterationShowList(projectSid);
    }

    /**
     * 展示迭代详细信息
     */
    @Override
    public IterationShowVo iterationShow(String projectId) {
        return projectMapper.getIterationShow(projectId);
    }

//    /**
//     * 修改迭代详细信息
//     */
//    @Override
//    public void iterationChange(String projectId, String projectName, String projectManager, Integer projectStatus, String description, LocalDateTime startTime, LocalDateTime finishTime) {
//        //通过projectId寻找到需要修改的迭代
//        Project entity = projectMapper.selectById(projectId);
//        LocalDateTime dateTime = LocalDateTime.now();
//        long time = Duration.between(startTime,finishTime).toDays();
//        if(time<0){
//            throw new ServiceException("结束时间早于开始时间");
//        } else {
//            entity.setProjectId(projectId).setProjectName(projectName).setProjectManager(projectManager).setProjectStatus(projectStatus).setDescription(description).setStartTime(startTime).setFinishTime(finishTime).setUpdateTime(dateTime);
//            projectMapper.updateById(entity);
//        }
//    }

//    /**
//     * 删除迭代
//     */
//    @Override
//    public void iterationRemove(String projectId) {
//        if (projectId == null) {
//            throw new ServiceException("无删除项");
//        } else {
//            projectMapper.deleteById(projectId);
//        }
//    }
}
